package com.project.service.impl;

import com.project.bean.FacilityBean;
import com.project.mapper.IFacilityDao;
import com.project.mapper.IRoomDao;
import com.project.service.IFacilityService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class FacilityImpl extends BaseService implements IFacilityService {

    @Override
    public List<FacilityBean> findByRoom(Integer roomId) {
        SqlSession session = this.getSession();
        IFacilityDao facilityDao = session.getMapper(IFacilityDao.class);
        List<FacilityBean> list = facilityDao.findByRoom(roomId);
        session.close();
        return list;
    }

    @Override
    public void damageInfo(FacilityBean facilityBean, Integer roomId) {
        SqlSession session = this.getSession();
        IFacilityDao facilityDao = session.getMapper(IFacilityDao.class);
        IRoomDao roomDao = session.getMapper(IRoomDao.class);
        facilityDao.damageInfo(facilityBean,roomId);
        roomDao.updateState(roomId,"损坏");

        session.commit();;
        session.close();
    }

    @Override
    public void updateFacility(Integer facilityId,Integer roomId) {
        SqlSession session = this.getSession();
        IFacilityDao facilityDao = session.getMapper(IFacilityDao.class);
        IRoomDao roomDao = session.getMapper(IRoomDao.class);
        facilityDao.updateFacility(facilityId,"已解决");
        if (facilityDao.countRoom(roomId,"未解决")==0){
            roomDao.updateState(roomId,"正常");
        }
        session.commit();
        session.close();
    }
}
